package com.tg.app.activity.device.add;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.appbase.custom.constant.UserProtocolConstants;
import com.base.BaseActivity;
import com.tg.app.R;
import com.tg.app.helper.ActivityHelper;
import com.tange.base.toolkit.DimenUtil;
import com.tg.appcommon.android.ImageUtils;
import com.tg.appcommon.android.LanguageUtils;
import com.tg.appcommon.android.TGApplicationBase;
import com.tg.appcommon.android.TGLog;
import com.tange.base.toolkit.TGThreadPool;
import com.tg.appcommon.android.TGToast;
import com.tg.appcommon.util.GoogleUtils;
import com.tg.data.http.entity.EmptyBean;
import com.tange.core.backend.service.http.ClientObserver;
import com.tg.network.socket.http.TGHttp;

import java.util.HashMap;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class ReAddFaildActivity extends BaseActivity implements View.OnClickListener {
    private static final String IMG_URL = "https://test-media-cloud-other-public.tange365.com/cloud_server/app/images/device_share/2021/04/23/客厅-share_device_to_an_account.png";
    private String mUUID;
    private TextView mTvDeviceOwner1;
    private String mShareImgUrl;
    private String mImgPath;

    @Override
    public boolean shouldSetStatusBarColor() {
        return true;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//remove title bar  即隐藏标题栏

        int h = getResources().getDisplayMetrics().heightPixels;
        float height = DimenUtil.px2dp(this, h);
        setContentView(R.layout.activity_faild_readd_device);

        initView();
        if (getIntent() != null){
            mUUID = getIntent().getStringExtra(ApSetWifiActivityEx.EXT_WIFI_UUID);
        }
        getShareImageUrl();
        hideActionBar();
        getDeviceOwner();
        mShareImgUrl = IMG_URL;
    }

    protected void initView() {
        mTvDeviceOwner1 = findViewById(R.id.app_name);
        findViewById(R.id.back_toolbar).setOnClickListener(v -> ActivityHelper.gotoDeviceListPage(ReAddFaildActivity.this));

        findViewById(R.id.add).setOnClickListener(v -> ActivityHelper.jump2ResetDeviceActivity(ReAddFaildActivity.this, mUUID, null));

        findViewById(R.id.add_wechat).setOnClickListener(v -> {
            sendShare();
        });

        modifyToolBar(null, false);
        String language = LanguageUtils.getLanguage(TGApplicationBase.getApplicationContext());
        if (!TextUtils.isEmpty(language) && !language.startsWith("zh-cn")) {
            Button button = findViewById(R.id.add_wechat);
            button.setText(R.string.txt_mail_request);
            @SuppressLint("UseCompatLoadingForDrawables") Drawable drawable = getResources().getDrawable(R.mipmap.icon_mail);
            button.setCompoundDrawables(drawable, null, null, null);
        }
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.back_toolbar) {
            finish();
        } else if (id == R.id.rl_about_layout) {
            if (GoogleUtils.isGoogleChannel()){
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse("market://details?id=" + getPackageName()));
                if (intent.resolveActivity(getPackageManager()) != null) { //可以接收
                    startActivity(intent);
                }
            }
        } else if (id == R.id.agreementtv) {
            ActivityHelper.getAgreementForWebView(this, UserProtocolConstants.USER_PROTOCOL);
        } else if (id == R.id.policytv) {
            ActivityHelper.getAgreementForWebView(this, UserProtocolConstants.PRIVACY);
        }
    }

    private void getDeviceOwner(){
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("uuid", mUUID);
        TGHttp.getInstance().getQueryOwner(hashMap)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ClientObserver<String>() {
                    @Override
                    protected void onSuccess(String content) {
                        TGLog.d("content = " + content);
                        if (content != null) {
                            String info1 = String.format(getString(R.string.txt_device_owner_info_1), content);
                            mTvDeviceOwner1.setText(info1);
                        }
                    }

                    @Override
                    protected void onResponseError(int errorCode, String errorInfo) {
                        showToast(errorInfo);
                    }
                });
    }

    private void sendShare(){
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("uuid", mUUID);
        TGHttp.getInstance().getShareApp(hashMap)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ClientObserver<EmptyBean>() {
                    @Override
                    protected void onSuccess(EmptyBean emptyBean) {
                    }

                    @Override
                    protected void onResponseError(int errorCode, String errorInfo) {
                        TGLog.d("errorInfo = " + errorInfo);
                        TGToast.showToast(errorInfo);
                    }

                    @Override
                    protected void onOtherError(String error) {
                        TGLog.d("error = " + error);
                    }

                    @Override
                    protected void onFinish() {
                    }
                });
    }

    private void downloadImp(){
        TGThreadPool.execute(() -> {
            mImgPath = ImageUtils.getDownloadPath(mShareImgUrl);
            TGLog.d("path ===" + mImgPath);
        });
    }

    private void getShareImageUrl(){
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("uuid", mUUID);
        TGHttp.getInstance().getShareImg(hashMap)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ClientObserver<String>() {
                    @Override
                    protected void onSuccess(String content) {
                        mShareImgUrl = content;
                        downloadImp();
                    }
                    @Override
                    protected void onResponseError(int errorCode, String errorInfo) {
                        downloadImp();
                    }
                });
    }
}
